<?php

namespace app\api\controller;

use app\BaseController;
use app\lib\tool\Str;
use app\api\model\Type as TypeModel;
use app\api\model\Article as ArticleModel;
use app\api\model\View as ViewModel;
use app\api\model\Favorite as FavoriteModel;
use app\api\model\Star as StarModel;

class Article extends BaseController
{
    public function initialize()
    {
        $this->uid = $this->request->uid;
    }

    /* 首页列表 */
    public function list()
    {
        $data = ArticleModel::order('id desc')->limit(4)->select();
        if ($data) {
            return show(200, 'success', $data);
        } else {
            return show(config('status.get_info_error'), '没有更多数据');
        }
    }

    /* 分类 */
    public function type()
    {
        $type = input('type', 0, 'intval');
        $type = ($type == 1) ? 5 : 6;
        $data = TypeModel::where('pid', $type)->order('sort asc')->select();
        if ($data) {
            return show(200, 'success', $data);
        } else {
            return show(config('status.get_info_error'), '没有更多数据');
        }
    }

    /* 热门推荐 */
    public function hot()
    {
        $type = input('type', 0, 'intval');
        $page = input('page', 1, 'intval');
        $perpage = 12;
        $type = ($type == 1) ? 5 : 6;
        $arr = TypeModel::where('pid', $type)->column('id');
        $ids = implode(',', $arr);
        $data = ArticleModel::whereIn('type', $ids)->order('view desc')->page($page, $perpage)->select()->toArray();
        if ($data) {
            return show(200, 'success', $data);
        } else {
            return show(config('status.get_info_error'), '没有更多数据');
        }
    }

    /* 分类列表 */
    public function typeList()
    {
        $type = input('tid', 0, 'intval');
        $page = input('page', 1, 'intval');
        $perpage = 12;
        $data = ArticleModel::where('type', $type)->order('view desc')->page($page, $perpage)->select()->toArray();
        if ($data) {
            return show(200, 'success', $data);
        } else {
            return show(config('status.get_info_error'), '没有更多数据');
        }
    }

    /* 更多列表 */
    public function moreList()
    {
        $page = input('page', 1, 'intval');
        $perpage = 12;

        $arr = TypeModel::whereIn('pid', [5, 6])->column('id');
        $ids = implode(',', $arr);
        $data = ArticleModel::whereIn('type', $ids)->order('view desc')->page($page, $perpage)->select()->toArray();
        if ($data) {
            return show(200, 'success', $data);
        } else {
            return show(config('status.get_info_error'), '没有更多数据');
        }
    }

    /* 详情 */
    public function getOne()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $data = ArticleModel::where('id', $id)->find();
        if ($data) {
            ArticleModel::update(['view' => $data['view'] + 1], ['id' => $id]);

            $favorite = FavoriteModel::where(['uid' => $this->uid, 'type' => 3, 'rid' => $id])->find();
            $data['is_favorite'] = ($favorite) ? true : false;
            $star = StarModel::where(['uid' => $this->uid, 'type' => 3, 'rid' => $id])->find();
            $data['is_star'] = ($star) ? true : false;

            $one = ViewModel::where(['uid' => $this->uid, 'type' => 3, 'rid' => $id])->find();
            if (!$one) {
                ViewModel::create([
                    'uid' => $this->uid,
                    'type' => 3,
                    'rid' => $id,
                    'time' => time()
                ]);
            } else {
                ViewModel::update([
                    'time' => time()
                ], [
                    'uid' => $this->uid,
                    'type' => 3,
                    'rid' => $id,
                ]);
            }
            $data['content'] = Str::fontToSpan($data['content']);
            return show(200, 'success', $data);
        } else {
            return show(config('status.get_info_error'), '没有更多数据');
        }
    }
}
